Stepan
successfully passed the interview and has been working for four months at one
of the most prestigious IT companies. It’s time to submit the project to the
manager, and Stepan, like a true student, is doing everything the night before
the deadline. He types the text incredibly fast but inattentively. This time,
while typing the last part of the text, he accidentally pressed the Caps
Lock key and didn’t notice. As a result, uppercase letters became lowercase,
and lowercase letters became uppercase. The other characters were typed
correctly.
Stepan is so
tired that he doesn't have the energy to fix the mistakes, so he decided to
take a nap.
Help Stepan:
write a program that will fix the inattentively typed text while he's resting.
Input. One string
containing the text that Stepan typed inattentively. The length of the string
does not exceed 500 characters.
Output. Print the
corrected text.
Sample
input |
Sample
output |
sCHOOL |
School |
string
Algorithm analysis
Read the input text
character by character.
·
If the character ch is a lowercase letter,
convert it to uppercase using the formula ch + ‘A’ – ‘a’.
·
If the character ch is an uppercase letter,
convert it to lowercase using the formula ch + ‘a’ – ‘A’.
Process the
input text character by character: convert lowercase letters to uppercase and
uppercase letters to lowercase.
while(scanf("%c",&ch)
== 1)
{
if(ch >= 'a' && ch <= 'z')
ch = ch
+ 'A' - 'a';
else if(ch >= 'A'
&& ch <= 'Z')
ch = ch + 'a'
- 'A';
printf ("%c",ch);
}
Java implementation – char array
import
java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner con = new Scanner(System.in);
char s[] = con.nextLine().toCharArray();
for(int i = 0; i < s.length; i++)
{
if (Character.isUpperCase(s[i]))
System.out.print(Character.toLowerCase(s[i]));
else if(Character.isLowerCase(s[i]))
System.out.print(Character.toUpperCase(s[i]));
else
System.out.print(s[i]);
}
con.close();
}
}
Java implementation – String
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner con = new Scanner(System.in);
String in = con.nextLine();
String out = "";
for(int i = 0; i < in.length(); i++)
{
if (Character.isUpperCase(in.charAt(i)))
out = out + Character.toLowerCase(in.charAt(i));
else if(Character.isLowerCase(in.charAt(i)))
out = out + Character.toUpperCase(in.charAt(i));
else
out = out + in.charAt(i);
}
System.out.println(out);
con.close();
}
}
Python implementation
Read the input string. Convert it into a
list of characters.
lst = list(input())
Convert lowercase letters in the list lst
to uppercase and uppercase letters to lowercase.
for i in range(len(lst)):
if lst[i].isupper():
lst[i] = lst[i].lower()
else:
lst[i] = lst[i].upper()
Convert the list of characters lst
into a string s.
s = "".join(lst)
Print the answer.
print(s)
Python implementation – list comprehension
Read the input string.
s = input()
Create a list where lowercase
letters become uppercase, and uppercase letters become lowercase.
res = "".join([ch.lower() if ch.isupper() else ch.upper() for ch in
s])
Print the answer.
print(res)
Python implementation – swapcase
Read the input string.
s = input()
Change the case of all letters in
the string: lowercase letters become uppercase, and uppercase letters become
lowercase.
res = s.swapcase()
Print the answer.
print(res)